
import os
import uuid
from ffmpy import FFmpeg
 
 
# 视频裁剪，输入 开始位置，宽高， (0,0), (100, 200)
# 
def cut_out_video(video_path: str, output_dir: str, start_pix: tuple, size: tuple):
    ext = os.path.basename(video_path).strip().split('.')[-1]
    if ext not in ['mp4', 'avi', 'flv']:
        raise Exception('format error')
    result = os.path.join(output_dir, '{}.{}'.format(uuid.uuid1().hex, ext))
    ff = FFmpeg(inputs={video_path: None},
                outputs={
                    result: '-vf crop={}:{}:{}:{} -y -threads 5 -preset ultrafast -strict -2'.format(size[0], size[1],
                                                                                                     start_pix[0],
                                                                                                     start_pix[1])})
    print(ff.cmd)
    ff.run()
    return result

# ffmpeg -ss 1:05 -i input.mp4 -t 10 -c:v copy -c:a copy output.mp4
# 截取时间 time_param:(开始时间，持续时间s) ， id文件名称
def cut_time_video(video_path: str, output_dir: str, time_param, id ):
    ext = os.path.basename(video_path).strip().split('.')[-1]
    if ext not in ['mp4', 'avi', 'flv']:
        raise Exception('format error')
    result = os.path.join(output_dir, '{}.{}'.format( id , ext)) 

    cmd = 'ffmpeg -ss {} -i "{}" -t {} -c:v copy -an {}'.format(time_param[0], video_path,  time_param[1], result)  # -an无音频
    os.system(cmd)
    return result


 
if __name__ == '__main__':
    # 裁剪
    # print(cut_out_video(r'C:/Users/hhq/Desktop/vids/VID20230605204932.mp4', r'./输出',   (0,0), (100, 200)    )) 
    pass 

    # 剪辑
    # print(cut_time_video(r'F:/soft/DownKyi/Media/生命治愈时刻，日出和星空下的“滑雪美梦”/1-生命治愈时刻，日出和星空下的“滑雪美梦”-1080P 高清-AVC.mp4', 
    #                      r'./输出',
    #                      (600, 20), 2 ) )
